package com.example.alarm.mvvm.adapter

import android.app.Activity
import android.text.Spannable
import android.text.SpannableString
import android.text.TextUtils
import android.text.style.UnderlineSpan
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.example.alarm.R
import com.example.alarm.mvvm.activity.PreviewImageActivity
import com.example.alarm.mvvm.bean.AlarmDetailBean
import com.gsafety.libraryktx.activity.CommonWebActivity
import com.gsafety.libraryktx.util.AppUtil

/**
 * 附件图片Adapter
 */
class RvShowWordAdapter(data: List<AlarmDetailBean.DocsBean>?) : BaseQuickAdapter<AlarmDetailBean.DocsBean, BaseViewHolder>(R.layout.alarm_item_show_word_layout, data) {
    override fun convert(helper: BaseViewHolder, item: AlarmDetailBean.DocsBean) {
        helper.getView<View>(R.id.cll_word_parent).setOnClickListener {
            var url = item.urlAddr.toString()
            var sufx = url.substring(url.lastIndexOf(".") + 1)
            if(sufx == "pdf"||sufx == "doc"||sufx == "xlsx"||sufx == "xls"||sufx == "docx"){
                CommonWebActivity.startIntent(mContext, item?.fileName!!, url)
            }

            if(sufx == "png"||sufx == "jpg"||sufx == "jpeg"){
                var fileList = ArrayList<AlarmDetailBean.DocsBean>()
                fileList.add(item)
                PreviewImageActivity.startIntent(mContext as Activity,fileList,0)
            }

        }
        var lastStr: String? = item?.fileName
        if (!TextUtils.isEmpty(lastStr)) {
            var index: Int? = lastStr?.lastIndexOf(".")
            if (-1 != index) {
                lastStr = lastStr?.substring(index!! + 1)
                when (lastStr) {
                    "doc" -> {
                        helper?.setImageResource(R.id.item_attach_pic_iv, R.mipmap.icon_word)
                    }
                    "docx" -> {
                        helper?.setImageResource(R.id.item_attach_pic_iv, R.mipmap.icon_word)
                    }
                    "xlsx" -> {
                        helper?.setImageResource(R.id.item_attach_pic_iv, R.mipmap.icon_excel)
                    }
                    "xls" -> {
                        helper?.setImageResource(R.id.item_attach_pic_iv, R.mipmap.icon_excel)
                    }
                    "png" -> {
                        helper?.setImageResource(R.id.item_attach_pic_iv, R.mipmap.icon_pic)
                    }
                    "jpg" -> {
                        helper?.setImageResource(R.id.item_attach_pic_iv, R.mipmap.icon_pic)
                    }
                    "jpeg" -> {
                        helper?.setImageResource(R.id.item_attach_pic_iv, R.mipmap.icon_pic)
                    }
                    "pdf" -> {
                        helper?.setImageResource(R.id.item_attach_pic_iv, R.mipmap.icon_pdf)
                    }
                    else ->{
                        helper?.setImageResource(R.id.item_attach_pic_iv, R.mipmap.icon_pic)
                    }
                }
            }
        }
        helper.setText(R.id.tv_name, item?.fileName)
    }
}
